package com.example.design.state.event;

import com.example.design.state.Context;
import com.example.design.state.Transition;
import com.example.design.state.enums.EventType;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 事件执行.
 *
 * @author duk
 * @date 2024-01-15 13:43
 */

@Slf4j
@Data
public abstract class Event {

    /**
     * 事件类型
     */
    private final Integer eventType;

    /**
     * 状态流转
     */
    private final Transition transition;

    protected Event(EventType eventType, Transition transition) {
        this.eventType = eventType.getType();
        this.transition = transition;
    }

    /**
     * 状态流转前操作
     */
    public void beforeTransit(Context ctx) {
        // 判断当前对象状态是否合法
        if (!transition.getCurrentState().equals(ctx.getDemand().getState())) {
            throw new IllegalArgumentException("需求当前状态[" + ctx.getDemand().getState() + "]不合法");
        }
    }

    /**
     * 状态流转
     */
    public void transit(Context ctx) {
        log.info("执行状态变更：{} -> {}", transition.getCurrentState(), transition.getNextState());
        ctx.getDemand().setState(transition.getNextState());
    }

    /**
     * 状态流转后操作
     */
    public void postTransit(Context ctx) {

    }

}
